package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryContTagRcordListReq;
import com.sg.dto.base.app.req.QueryContTagRcordDetailReq;
import com.sg.dto.base.app.req.QueryContTagRcordListReq;
import com.sg.entity.OmsBasicContentTagRecord;
import com.sg.service.base.app.MOmsBasicContentTagRecordService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsBasicContentTagRecordMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsBasicContentTagRecordServiceImpl
        extends ServiceImpl<OmsBasicContentTagRecordMapper, OmsBasicContentTagRecord>
        implements MOmsBasicContentTagRecordService {

    @Autowired
    OmsBasicContentTagRecordMapper omsBasicContentTagRecordMapper;

    /**
     * 3-13-04查业务应用内容标签记录列表[2243]
     * gen by moon at 10/22/2022, 10:09:52 PM
     */
    @Trace(operationName = "3-13-04查业务应用内容标签记录列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsBasicContentTagRecord> queryContTagRcordList(QueryContTagRcordListReq reqDto) {

        try {
            QueryWrapper<OmsBasicContentTagRecord> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsBasicContentTagRecord.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsBasicContentTagRecord.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsBasicContentTagRecord.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsBasicContentTagRecord>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-13-04批量查业务应用内容标签记录列表[2246]
     * gen by moon at 11/3/2022, 10:54:38 PM
     */
    @Trace(operationName = "3-13-04批量查业务应用内容标签记录列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsBasicContentTagRecord> batchQueryContTagRcordList(BatchQueryContTagRcordListReq reqDto) {

        try {
            QueryWrapper<OmsBasicContentTagRecord> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            // TODO: 2023/9/21 手动代码
            if (reqDto.getContTagRcordList() != null && !reqDto.getContTagRcordList().isEmpty()) {
                queryWrapper.in("tag_name", reqDto.getContTagRcordList());
            }

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsBasicContentTagRecord.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsBasicContentTagRecord.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsBasicContentTagRecord.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsBasicContentTagRecord>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-13-04新增业务应用内容标签记录[2758]
     * gen by moon at 10/13/2022, 10:49:16 PM
     */
    @Trace(operationName = "3-13-04新增业务应用内容标签记录")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addContTagRcord(OmsBasicContentTagRecord omsBasicContentTagRecord) {
        omsBasicContentTagRecord.setTagContentRecordId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsBasicContentTagRecord.setIsArchive("FALSE");
        omsBasicContentTagRecord.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsBasicContentTagRecord.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsBasicContentTagRecord.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsBasicContentTagRecord.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsBasicContentTagRecord.setIsValid("FALSE");
        omsBasicContentTagRecord.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsBasicContentTagRecord.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsBasicContentTagRecord)) {
            return omsBasicContentTagRecord.getTagContentRecordId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-13-04批量删业务应用内容标签记录[2765]
     * gen by moon at 10/13/2022, 10:49:17 PM
     */
    @Trace(operationName = "3-13-04批量删业务应用内容标签记录")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteContTagRcord(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 3-13-04修改业务应用内容标签记录[4716]
     * gen by moon at 10/13/2022, 10:49:17 PM
     */
    @Trace(operationName = "3-13-04修改业务应用内容标签记录")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateContTagRcord(OmsBasicContentTagRecord omsBasicContentTagRecord) {

        return super.updateById(omsBasicContentTagRecord);
    }

    /**
     * 3-13-04查询业务应用内容标签记录详情[4717]
     * gen by moon at 10/13/2022, 10:49:18 PM
     */
    @Trace(operationName = "3-13-04查询业务应用内容标签记录详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsBasicContentTagRecord queryContTagRcordDetail(QueryContTagRcordDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsBasicContentTagRecord> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsBasicContentTagRecord> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsBasicContentTagRecord retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-13-04批量改指标模板标签记录[4990]
     * gen by moon at 10/23/2022, 2:30:45 PM
     */
    @Trace(operationName = "3-13-04批量改指标模板标签记录")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateTargetTempTagRcord(List<OmsBasicContentTagRecord> omsBasicContentTagRecord) {

        if (omsBasicContentTagRecord == null || omsBasicContentTagRecord.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsBasicContentTagRecord);

    }

}

